package com.ht.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hejialun
 * @ClassName FileUtil
 * @description: TODO（文件工具类）
 * @Date: 2024/6/6 16:21
 */
public class FileUtil {
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    //支持转pdf的文件类型
    private static final List<String> supportToPdfFileType = Arrays
            .asList("doc", "docx", "xls", "xlsx", "txt");

    /**
     * 获取文件名称-去掉后缀
     *
     * @param fileName：文件原名称
     * @return
     */
    public static String getFileNameWithoutExtension(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return "";
        }
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return fileName;
        }
        return fileName.substring(0, dotIndex);
    }

    /**
     * 获取文件后缀-不带小数点
     *
     * @param fileName
     * @return
     */
    public static String getFileNameSuffix(String fileName) {
        //判断文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return suffix;
    }

    /**
     *把InputStream转换成byte数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, bytesRead);
        }
        return buffer.toByteArray();
    }

}
